package com.linb.pan.storage.engine.oss.initial;

import com.aliyun.oss.OSSClient;
import com.linb.pan.core.exception.BPanFrameworkException;
import com.linb.pan.storage.engine.oss.config.OssStorageEngineConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OssBucketInitializer implements CommandLineRunner {

    @Autowired
    private OssStorageEngineConfig config;

    @Autowired
    private OSSClient client;

    @Override
    public void run(String... args) throws Exception {
        boolean bucketExist = client.doesBucketExist(config.getBucketName());
        if (bucketExist) {
            log.info("存在桶:{}", config.getBucketName());
        }
        if (!bucketExist && config.getAutoCreateBucket()) {
            client.createBucket(config.getBucketName());
            log.info("桶不存在，创建桶");
        }
        if (!bucketExist && !config.getAutoCreateBucket()) {
            throw new BPanFrameworkException("桶不存在!!请手动创建桶");
        }
    }
}
